package tech.aistar.controller.WXdaoyou;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tech.aistar.config.WxInterceptor;
import tech.aistar.entity.Wx.Article;
import tech.aistar.entity.Wx.WxOrders;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.daoyou.GuideApply;
import tech.aistar.entity.daoyou.GuideCodeStatus;
import tech.aistar.entity.daoyou.GuideResult;
import tech.aistar.entity.person_center.Orders;
import tech.aistar.service.daoyou.GuideService;
import tech.aistar.service.wxdaoyou.Wxservice;
import tech.aistar.util.SmsModel;
import tech.aistar.util.WxNew.GetNew;
import tech.aistar.util.date_SimpleDateFormat.DateUtileDemo;
import tech.aistar.util.mytoken.CreatToken;
import tech.aistar.util.mytoken.WxToken;
import tech.aistar.vo.daoyou.GuideOrdersAll;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wx")
public class wxlogin {

    @Autowired
    private Wxservice wxservice;
    @Autowired
    GuideService service;

    @RequestMapping("/checkToken")
    public     @ResponseBody String checkToken(String token){
       if(!"error".equals( CreatToken.checkToken(token))){
           return "1";
       }else {
           return "error";
       }
    }


    @RequestMapping("/login")
    public @ResponseBody
     GuideResult login(String phone,String password){
       UserTable userTable= wxservice.wxgetByPhone(phone,password);
       if(userTable==null){
           return new GuideResult(GuideCodeStatus.ERROR,false,"账号或密码错误");
       }else {
       String token=  CreatToken.createToken(userTable.getPhone());
           return new GuideResult(GuideCodeStatus.OK,true,"成功",token);
       }
    }

    //获取个人账单
    @RequestMapping("/getBill")
    @WxInterceptor
    public  @ResponseBody GuideResult getBill(String token){
       String tokens=  CreatToken.checkToken(token);
//        if("error".equals(tokens)){
//            return new GuideResult(GuideCodeStatus.ERROR,false,"非法登陆");
//        }else {
           List<GuideOrdersAll> list= wxservice.wxgetOrder(tokens);
           return new GuideResult(GuideCodeStatus.OK,true,"成功",list);
//        }

    }

    //获取所有的申请列表
    @RequestMapping("/checkBill")
    @WxInterceptor
    public @ResponseBody GuideResult checkBill(String token){
        String tokens=  CreatToken.checkToken(token);
//        if("error".equals(tokens)){
//            return new GuideResult(GuideCodeStatus.ERROR,false,"非法登陆");
//        }else {
            Date date = new Date();
            String strDateFormat = "yyyy-MM-dd ";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            String  ordertime= sdf.format(date);
            Integer id= wxservice.getGuideId(tokens); //获取的是导游的id
            List<GuideOrdersAll> list=service.getBill(id,ordertime);

            if(list.size()==0){
                return new GuideResult(GuideCodeStatus.OK,false,"没有数据");
            }else{
                return new GuideResult(GuideCodeStatus.OK,true,"成功",list);
            }
//        }
    }
    @RequestMapping("/OrderLineBill")
    @WxInterceptor
    public  @ResponseBody GuideResult OrderLine(String token,Integer tid){
        String tokens=  CreatToken.checkToken(token);
//        if("error".equals(tokens)){
//            return new GuideResult(GuideCodeStatus.ERROR,false,"非法登陆");
//        }else {
            Integer id= wxservice.getGuideId(tokens); //获取的是导游的id
            Orders orders=service.getBillById(tid);
            String starttime= DateUtileDemo.format("yyyy-MM-dd",orders.getApplyTime());
            String endtime=DateUtileDemo.format("yyyy-MM-dd",orders.getPlayDay());
            java.sql.Date sqlend= java.sql.Date.valueOf(starttime);
            Boolean flag= service.getOrderById(id, starttime,sqlend);
            if(flag==true){
                service.accessBill(tid);
                //短信通知
                SmsModel.sendSms(orders.getPhoneNumber(),441664,orders.getApplyName());
                return new GuideResult(GuideCodeStatus.OK,true,"ok");
            }else{
                return new GuideResult(GuideCodeStatus.OK,false,"接单时间冲突");
            }
//        }
    }

    //个人Home
    @RequestMapping("/home")
    @WxInterceptor
    public @ResponseBody GuideResult getMySelf(String token){
        String tokens=  CreatToken.checkToken(token);
        GuideApply guideApply= wxservice.getGuideById(tokens);
        if(guideApply==null){
            return new GuideResult(GuideCodeStatus.OK,false,"不是导游");
        }
        return new GuideResult(GuideCodeStatus.OK,true,"是导游",guideApply);
    }

//    用户核销码
    @RequestMapping("/tour")
    @WxInterceptor
    public @ResponseBody GuideResult getTour(String token,Integer BillId){

        Orders orders=   wxservice.getOrders(BillId);

        if(orders==null){
            return new GuideResult(GuideCodeStatus.ERROR,false,"改账单错误");
        }else {
           String sing= WxToken.createToken(orders.getId().toString());//封装的是账单的id
            WxOrders wxOrders= new WxOrders();
            wxOrders.setOrders(orders);
            wxOrders.setSing(sing);
            return new GuideResult(GuideCodeStatus.OK,true,"账单正确",wxOrders);
        }
    }


    @RequestMapping("/delBill")
    @WxInterceptor
    public @ResponseBody GuideResult DelBill(String token,String text){
        String[] billInfomation=text.split("@");
        if(billInfomation==null||billInfomation.length == 0 ){
            return new GuideResult(GuideCodeStatus.ERROR,false,"无效二维码");
        }
        Integer billId=null;
        try{
         billId= Integer.valueOf(billInfomation[1]);
        }catch (Exception e){
            return new GuideResult(GuideCodeStatus.ERROR,false,"无效二维码");
        }

       String delBillStart = wxservice.delBill(billInfomation[0],billId);

        if(delBillStart.equals("error"))  {
            return new GuideResult(GuideCodeStatus.ERROR,false,"无效二维码");

        }else{
            return new GuideResult(GuideCodeStatus.OK,true,"核销成功");
        }
    }

    //检测状态
    @RequestMapping("/getBillStart")
    @WxInterceptor
    public @ResponseBody GuideResult getBillStart(String token,String sing){
       String singStart= WxToken.wxcheckToken(sing);

       if("error".equals(singStart)){
           return new GuideResult(GuideCodeStatus.ERROR,false,"签名错误");
       }
       Integer BillId=Integer.valueOf(singStart);
       Orders orders= wxservice.checkBill(BillId);

       if( orders!=null&&orders.getOrderStatus()==2){
           return new GuideResult(GuideCodeStatus.OK,true,"核销成功");
       }
        return new GuideResult(GuideCodeStatus.ERROR,false,"二维码过期");
    }


    @RequestMapping("/commont")
    @WxInterceptor
    public @ResponseBody GuideResult setCommont(String token,Integer id,String text,Integer num){
        try{
            wxservice.setOrder(id,text,num);
            return new GuideResult(GuideCodeStatus.OK,true,"评论成功");
        }catch (Exception e){
            return new GuideResult(GuideCodeStatus.ERROR,false,"内部错误");
        }
    }

    @RequestMapping("/new")
    @WxInterceptor
    public @ResponseBody GuideResult getOneNew(){
        try{
            List<Article> arrayList= GetNew.getNewOneDay();
            return new GuideResult(GuideCodeStatus.OK,true,"获取成功",arrayList);
        }catch (Exception e){
            return new GuideResult(GuideCodeStatus.ERROR,false,"获取成功");
        }
    }



}
